<?php
declare (strict_types = 1);

namespace thinkless\common;

use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 控制器基础类
 */
abstract class BaseCommand extends Command {
    
    /**
     * @var Input
     */
    protected $input = null;

    /**
     * @var Output
     */
    protected $output = null;

    /**
     * 读取一行交互
     */
    protected function readLine($message = '') {
        print($message);
        $handle = fopen('php://stdin','r');
        $input = fgets($handle);
        fclose($handle);
        return trim($input);
    }

    protected function readYN($message, $empty = false) {
        $message = $message . '【Y'. ($empty ? '(默认)' : '' ) .'/N】:';
        $res = $this->readLine($message);
        if($empty === true && empty($res)) return true;
        return $res == 'Y';
    }

    /**
     * 命令行安装
     */
    protected function execute(Input $input, Output $output) {
        $this->input = $input;
        $this->output = $output;
        $this->start();
    }

    abstract protected function start();

    protected function info($message) {
        $this->output->info($message);
    }
}